package com.deyuanyun.pic.insp.controller.inspresult;

import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ResponseUtil;
import com.deyuanyun.pic.insp.util.InspExportUtil;
import com.deyuanyun.pic.settings.controller.AbstractBaseController;
import com.deyuanyun.pic.insp.controller.dto.InspresultByFind;
import com.deyuanyun.pic.settings.service.base.CodeService;
import com.deyuanyun.pic.insp.service.inspresult.MetalLossService;
import com.deyuanyun.pic.settings.support.utils.ExcelUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * 内检测  检测数据   金属损失
 *
 * @author    scalpel.Wang
 * @version   2015年10月8日
 * @see         // 引用的类
 * Method List:
 *         1.toPage   ---------------- 去主页
 *         2.search   ---------------- 搜索数据
 *         3.exportExl---------------- 导出数据
 */
@Controller
@RequestMapping("/insp/metalLoss")
public class MetalLossController extends AbstractBaseController {

	@Resource
	MetalLossService metalLossService;

	@Resource
	CodeService codeService;


	@RequestMapping("/page.html")
	public ModelAndView toPage(HttpServletRequest request,String lineId,String taskSegmentId){
		ModelAndView mav=new ModelAndView();

		//查询字典数据 金属损伤类型
		List<HashMap> codeList= codeService.selectList("METALOSS_TYPE");

		//查询管道名称
		List lineList=lineService.getLine();

		mav.addObject("codeList", codeList);
		mav.addObject("lineList", lineList);



		HashMap map= RequestUtil.parseRequestParamToHashMap(request);
		mav.addAllObjects(map);

		mav.setViewName("/insp/inspresult/metalLoss");
		return mav;

	}


	/**
	 *
	 * 查询金属损伤数据
	 *
	 * @param current 当前页 pageCount每页显示数量
	 * @return    // 方法返回值的说明
	 *
	 *
	 */
	@RequestMapping("/search.json")
	public void search(@RequestParam(required = false) Integer current,
					   @RequestParam(required = false) Integer pageCount,
					   HttpServletRequest request,
					   HttpServletResponse response){

		int pageNo=1;
		int pageSize=10;
		if(null != current)
		{
			pageNo=current;
		}

		if(null != pageCount)
		{
			pageSize=pageCount;
		}
		PageHelper.startPage(pageNo, pageSize);
		HashMap map= RequestUtil.parseRequestParamToHashMap(request);
		Page main=metalLossService.findPage(map);
		ResponseUtil.printl(response, ResponseUtil.getJson(main).toString(), "JSON");
	}

	/**
	 *
	 * 导出金属损伤数据
	 *
	 * @param request 当前页 pageCount每页显示数量
	 * @return    // 方法返回值的说明
	 *
	 *
	 */
	@RequestMapping("/exportExl")
	public void exportExl(HttpServletRequest request,HttpServletResponse response){
			HashMap param= RequestUtil.parseRequestParamToHashMap(request);

		List result=metalLossService.findObjectList(param);
		param.put("data",result);
		try {
			InspExportUtil.exportExcel(request.getRealPath("/")+"/WEB-INF/classes/template/excl/insp/metalloss.xlsx",param,response,"金属损失");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@RequestMapping("/exportApprarse")
	public void exportApprarse(HttpServletRequest request,HttpServletResponse response){

		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		InputStream filePath =this.getClass().getResourceAsStream("/template/excl/insp/apprarse.xls");
		int[] lineStart = new int[] { 4 };
		List<List<String[]>> result=metalLossService.queryExportApprarse(param);
		InspExportUtil.exportApprarse( "金属损失", result, response,request);
	}
	/**
	 * @Description: 跳转到：查询统计 -> 金属损失页面
	 * @Author: 荣华兴
	 * @Create Date: 2015年9月24日
	 * @参数：@return
	 * @Return:String
	 */
	//@RequestMapping("/toMetlLoss")
	public ModelAndView toMetalLoss(InspresultByFind IRBF) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("lineList", lineService.getLine());
		mav.addObject("line", lineService.getLine());
		mav.setViewName("/insp/statistics/metalLoss");
		return mav;
	}

	/**
	 * @Description: 根据检测管段ID查询检测任务、物理管段
	 * @Author: 荣华兴
	 * @Create Date: 2015年9月28日
	 * @参数：@return
	 * @Return:ModelAndView
	 */
	@RequestMapping("/findTaskAndSegmentByTaskLineId")
	public ModelAndView findTaskAndSegmentByTaskLineId(String TaskLineId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("task", metalLossService.getTaskByTaskLineId(TaskLineId));
		mav.addObject("Segment",metalLossService.getSegmentByTaskLineId(TaskLineId));
		return mav;
	}

	/**
	 * @Description: 查询统计-金属损失所需要展示统计的值
	 * @Author: 荣华兴
	 * @Create Date: 2015年9月24日
	 * @参数：@param IRBF：管道ID,检测段ID，检测任务，物理管段
	 * @参数：@return
	 * @Return:ModelAndView
	 */
	@RequestMapping("/MetlLossAmount")
	public ModelAndView MetalLossAmount(InspresultByFind IRBF) {
		ModelAndView mav = new ModelAndView();
		//总页查询
		mav.addObject("map",metalLossService.MetalLossCount(IRBF));
		// 子页面，饼图
		mav.addObject("cmap", metalLossService.MetalLossAmount(IRBF));
		mav.addObject("smap",metalLossService.MetalLossAmount(IRBF));
		// 时钟图查询的值
		mav.addObject("tmap",metalLossService.getCountBytimap(IRBF));
		// 柱状图
		mav.addObject("bar",metalLossService.findMetalCountForBar(IRBF));

		mav.setViewName("/insp/statistics/metalLoss");
		return mav;
	}

	/**
	 *测试页
	 */
	@RequestMapping("/toTest")
	public ModelAndView toTest(InspresultByFind IRBF) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("tmap",metalLossService.getCountBytimap(IRBF));

		mav.setViewName("/insp/statistics/EchartsDemo");
		return mav;
	}

}
